home *** CD-ROM | disk | FTP | other *** search
/ Die Ultimative Software-P…i Collection 1996 & 1997 / Die Ultimative Software-Pakete CD-ROM fur Atari Collection 1996 & 1997.iso / p / portfoli / small_c / calc.sci next >
Encoding:
Text File  |  1996-10-30  |  1.5 KB  |  98 lines

  1. char *Lineptr;
  2. int Stack[10], Stackptr, Stacktop;
  3.  
  4. calc()
  5. {
  6.    char line[80];
  7.    char c;
  8.  
  9.    Stacktop = 10;
  10.    for(;;)
  11.    {
  12.       puts("-> ");
  13.       if( gets(Lineptr=line) )
  14.       {
  15.          if( *Lineptr=='x' )
  16.             return;
  17.          addition();
  18.          printf( "%d\n", pop() );
  19.       }
  20.    }
  21. }
  22.  
  23. number()
  24. {
  25.    if( isdigit( *Lineptr ) )
  26.    {
  27.       push( atoi( Lineptr ) );
  28.       while( isdigit( *Lineptr ) )
  29.          ++Lineptr;
  30.    }
  31. }
  32.  
  33. addition()
  34. {
  35.    int num;
  36.  
  37.    for(;;)
  38.    {
  39.       multiplication();
  40.       if( *Lineptr=='+' )
  41.       {
  42.          ++Lineptr;
  43.          multiplication();
  44.          push( pop() + pop() );
  45.       }
  46.       else if ( *Lineptr=='-' )
  47.       {
  48.          ++Lineptr;
  49.          multiplication();
  50.          num = pop();
  51.          push( pop() - num );
  52.       }
  53.       else
  54.          return;
  55.    }
  56. }
  57.  
  58. multiplication()
  59. {
  60.    int num;
  61.  
  62.    for(;;)
  63.    {
  64.       number();
  65.       if( *Lineptr=='*' )
  66.       {
  67.          ++Lineptr;
  68.          number();
  69.          push( pop() * pop() );
  70.       }
  71.       else if ( *Lineptr=='/' )
  72.       {
  73.          ++Lineptr;
  74.          number();
  75.          num = pop();
  76.          push( pop() / num );
  77.       }
  78.       else
  79.          return;
  80.    }
  81. }
  82.  
  83. push( n )
  84. {
  85.    if( Stackptr<Stacktop )
  86.       return Stack[ Stackptr++ ] = n;
  87.    puts( "stack overflow\n" );
  88. }
  89.  
  90. pop()
  91. {
  92.    if( Stackptr>0 )
  93.       return Stack[ --Stackptr ];
  94.    puts( "stack underflow\n" );
  95. }
  96.  
  97. isdigit(c){ return '0'<=c && c<='9';}
  98.